package cn.edu.jlu.community.app.services.express;

import android.app.AlertDialog;
import android.support.v4.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


import cn.edu.jlu.community.R;

/**
 * Created by lkk on 3/29/15.
 */
public class ResultFragment extends Fragment {

    private ListView lv;
    private ResultFollowAdapter adapter;
    private List<ResultEntity> rs=new ArrayList<ResultEntity>();
    private boolean sort_up = true;
    private   int state=0;

    public static final int STATE_RECEIVED = 3;
    public static final int STATE_ON_PASSAGE = 0;

    private String jsonStr;

    public Toast mToast=null;

    public static ResultFragment newInstance(String jsonStr) {
        ResultFragment fragment = new ResultFragment();
        Bundle nBundle = new Bundle();
        nBundle.putString("jsonStr", jsonStr);
        fragment.setArguments(nBundle);
        return fragment;
    }

    public ResultFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.result_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if(getArguments()!=null) {
            jsonStr = getArguments().getString("jsonStr");
            initView();
        }
    }

    private void initView(){
        lv=(ListView) getView().findViewById(R.id.followListView);
        lv.setAdapter(adapter);
        parse(jsonStr);
    }

    public void parse(String jsonStr){
        try {
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<ResultEntity>>() {}.getType();
            JSONObject obj = new JSONObject(jsonStr);
            if(obj.get("message").equals("ok")){
                rs = gson.fromJson(obj.getString("data"), type);
                Collections.reverse(rs);
                state = Integer.parseInt(obj.get("state").toString());
                adapter = new ResultFollowAdapter(getActivity(), rs, sort_up, state);
                lv.setAdapter(adapter);

                if(state == STATE_RECEIVED)
                {
                    //((TextView)getView().findViewById(R.id.follow_textview)).setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_success, 0);
                    getView().findViewById(R.id.colorLine).setBackgroundResource(R.drawable.red_line_green);
                    getView().findViewById(R.id.dot_full).setVisibility(View.VISIBLE);
                }
                if(state == STATE_ON_PASSAGE)
                {
                    getView().findViewById(R.id.colorLine).setBackgroundResource(R.drawable.red_line_blue);
                }

            }else{
                showToast(obj.getString("message"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void showToast(String msg) {

        if (mToast == null) {
            mToast = Toast.makeText(getActivity().getApplicationContext(), msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }
}
